//
//  SJSubmitVC.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/30.
//

#import "SJSubmitVC.h"
#import "SJSubmitView.h"

@interface SJSubmitVC (){
    NSMutableDictionary *_applyInfo;
}
@property (nonatomic, strong)SJSubmitView *headView;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SJSubmitVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = kLocalString(@"申请信息");
    self.view.backgroundColor = KBgColor;
    [self.view addSubview:self.tableView];
    _headView = [SJSubmitView viewFromXIB];
    _headView.frame = CGRectMake(0, 0, KScreenWidth,1100);
    kWeakSelf(self);
    [_headView setBtnBlock:^(NSInteger btnIndex) {
        [weakself hanldeBtnAction:btnIndex];
    }];
    self.tableView.tableHeaderView = _headView;
    
}

- (NSUInteger)ramdonInt{
    NSUInteger randomNumber = arc4random_uniform(9000000000) + 1000000000;
    
    return randomNumber;
}


-(void)hanldeBtnAction:(NSInteger)index{
    
    if (index == 1) {//提交
        if (!_headView.checkBtn.isSelected) {
            kShowToast(@"请先同意借款协议");
            return;
        }
        if (_headView.botTf1.text.length == 0) {
            kShowToast(@"请输入持卡人姓名");
            return;
        }
        if (_headView.botTf2.text.length == 0) {
            kShowToast(@"请选择收款银行");
            return;
        }
        if (_headView.botTf3.text.length == 0) {
            kShowToast(@"请输入银行卡号");
            return;
        }
        if (_headView.botTf4.text.length == 0) {
            kShowToast(@"请再次输入银行卡号");
            return;
        }
        if (![_headView.botTf3.text isEqualToString:_headView.botTf4.text]) {
            kShowToast(@"两次银行卡号输入不一致");
            return;
        }
        
        NSString *money = @"50,000";
        NSString *incomeMoney = @"50,000";
        NSString *lixi = @"2,500";
        NSString *backMoney = @"52,500";
        NSString *dateStr = [NSString dateStrFromDate:[[NSDate date] dateByAddingDays:91] formatStr:@"yyyy.MM.dd"];
        NSString *creatDate = [NSString dateStrFromDate:[NSDate date] formatStr:@"yyyy-MM-dd HH:mm"];
        NSMutableDictionary *applyDict = [NSMutableDictionary new];
        [applyDict setObject:money forKey:@"money"];
        [applyDict setObject:incomeMoney forKey:@"incomeMoney"];
        [applyDict setObject:dateStr forKey:@"dateStr"];
        [applyDict setObject:lixi forKey:@"lixi"];
        [applyDict setObject:backMoney forKey:@"backMoney"];
        [applyDict setObject:creatDate forKey:@"creatDate"];
        [applyDict setObject:kSTRING(@([self ramdonInt])) forKey:@"id"];
        
        [ZCTool showHudOnView:self.view];
        [ZKHttpManager postWithPath:@"appInfo" params:nil finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
            [ZCTool hideHudOnView:self.view];
            kShowToast(@"申请成功");
            NSString *key = [NSString stringWithFormat:@"curOrder_%@",kUserInfo.userId];
            [[NSUserDefaults standardUserDefaults] setObject:applyDict forKey:key];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.applyFinish) self.applyFinish(1);
                [self.navigationController popToRootViewControllerAnimated:YES];
            });
        }];
    }
}


#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight,kScreenWidth,kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

@end
